<?php
/* Author: tbloc@cantho.gov.vn */
class Xuat{  
//Properties
private $XUAT_ID;
private $TRUONG_ID;
private $TUAN_ID;
private $THANG_ID;
private $XUAT_SO_PHIEU_XUAT;
private $XUAT_NGAY_XUAT;
private $XUAT_SO_PHIEU_INT;
private $XUAT_TONG_TIEN_PHAI_THU;
private $XUAT_TONG_TIEN_DA_THU;
private $XUAT_LY_DO_XUAT;
private $IS_ACTIVE;
 private $MIN_XUAT_NGAY_XUAT;
    private $MAX_XUAT_NGAY_XUAT;
    public function getMIN_XUAT_NGAY_XUAT() {
        return $this->MIN_XUAT_NGAY_XUAT;
    }

    public function getMAX_XUAT_NGAY_XUAT() {
        return $this->MAX_XUAT_NGAY_XUAT;
    }

    public function setMIN_XUAT_NGAY_XUAT($MIN_XUAT_NGAY_XUAT) {
        $this->MIN_XUAT_NGAY_XUAT = $MIN_XUAT_NGAY_XUAT;
    }

    public function setMAX_XUAT_NGAY_XUAT($MAX_XUAT_NGAY_XUAT) {
        $this->MAX_XUAT_NGAY_XUAT = $MAX_XUAT_NGAY_XUAT;
    }

//properties set get 
public function setXUAT_ID($XUAT_ID) 
{
	$this->XUAT_ID = $XUAT_ID;
}
public function getXUAT_ID()
{
	return $this->XUAT_ID;
}	        
public function setTRUONG_ID($TRUONG_ID) 
{
	$this->TRUONG_ID = $TRUONG_ID;
}
public function getTRUONG_ID()
{
	return $this->TRUONG_ID;
}


public function setTHANG_ID($THANG_ID) 
{
	$this->THANG_ID = $THANG_ID;
}
public function getTHANG_ID()
{
	return $this->THANG_ID;
}

public function setTUAN_ID($TUAN_ID) 
{
	$this->TUAN_ID = $TUAN_ID;
}
public function getTUAN_ID()
{
	return $this->TUAN_ID;
}



public function setXUAT_SO_PHIEU_INT($XUAT_SO_PHIEU_INT) 
{
	$this->XUAT_SO_PHIEU_INT = $XUAT_SO_PHIEU_INT;
}
public function getXUAT_SO_PHIEU_INT()
{
	return $this->XUAT_SO_PHIEU_INT;
}	

public function setXUAT_SO_PHIEU_XUAT($XUAT_SO_PHIEU_XUAT) 
{
	$this->XUAT_SO_PHIEU_XUAT = $XUAT_SO_PHIEU_XUAT;
}
public function getXUAT_SO_PHIEU_XUAT()
{
	return $this->XUAT_SO_PHIEU_XUAT;
}
public function setXUAT_NGAY_XUAT($XUAT_NGAY_XUAT) 
{
	$this->XUAT_NGAY_XUAT = $XUAT_NGAY_XUAT;
}
public function getXUAT_NGAY_XUAT()
{
	return $this->XUAT_NGAY_XUAT;
}	        
public function setXUAT_TONG_TIEN_PHAI_THU($XUAT_TONG_TIEN_PHAI_THU) 
{
	$this->XUAT_TONG_TIEN_PHAI_THU = $XUAT_TONG_TIEN_PHAI_THU;
}
public function getXUAT_TONG_TIEN_PHAI_THU()
{
	return $this->XUAT_TONG_TIEN_PHAI_THU;
}	        
public function setXUAT_TONG_TIEN_DA_THU($XUAT_TONG_TIEN_DA_THU) 
{
	$this->XUAT_TONG_TIEN_DA_THU = $XUAT_TONG_TIEN_DA_THU;
}
public function getXUAT_TONG_TIEN_DA_THU()
{
	return $this->XUAT_TONG_TIEN_DA_THU;
}	        
public function setXUAT_LY_DO_XUAT($XUAT_LY_DO_XUAT) 
{
	$this->XUAT_LY_DO_XUAT = $XUAT_LY_DO_XUAT;
}
public function getXUAT_LY_DO_XUAT()
{
	return $this->XUAT_LY_DO_XUAT;
}	        
public function setIS_ACTIVE($IS_ACTIVE) 
{
	$this->IS_ACTIVE = $IS_ACTIVE;
}
public function getIS_ACTIVE()
{
	return $this->IS_ACTIVE;
}	        
//crud methods
public function insert($db = null) {
	if ($db != null){
	$sql = "INSERT INTO `xuat`(                        
			`XUAT_ID`, 
                         `TUAN_ID`,
                        `THANG_ID`,
				`TRUONG_ID`, 
				`XUAT_SO_PHIEU_XUAT`, 
                                `XUAT_SO_PHIEU_INT`,
				`XUAT_NGAY_XUAT`, 
				`XUAT_TONG_TIEN_PHAI_THU`, 
				`XUAT_TONG_TIEN_DA_THU`, 
				`XUAT_LY_DO_XUAT`, 
				`IS_ACTIVE` 
		)
	VALUES(
    		:XUAT_ID,  :TUAN_ID,
                        :THANG_ID,
	    		:TRUONG_ID, 
	    		:XUAT_SO_PHIEU_XUAT, 
                        :XUAT_SO_PHIEU_INT,
	    		:XUAT_NGAY_XUAT, 
	    		:XUAT_TONG_TIEN_PHAI_THU, 
	    		:XUAT_TONG_TIEN_DA_THU, 
	    		:XUAT_LY_DO_XUAT, 
	    		:IS_ACTIVE 
	    );";
	$parameters = array(
			":XUAT_ID", ":TUAN_ID",
                        ":THANG_ID",
				":TRUONG_ID", 
				":XUAT_SO_PHIEU_XUAT", 
                                ":XUAT_SO_PHIEU_INT",
				":XUAT_NGAY_XUAT", 
				":XUAT_TONG_TIEN_PHAI_THU", 
				":XUAT_TONG_TIEN_DA_THU", 
				":XUAT_LY_DO_XUAT", 
				":IS_ACTIVE" 
		);
    $values = array(  
    		$this->getXUAT_ID(), $this->getTUAN_ID(), $this->getTHANG_ID(), 
	    		$this->getTRUONG_ID(), 
	    		$this->getXUAT_SO_PHIEU_XUAT(), 
                        $this->getXUAT_SO_PHIEU_INT(),
	    		$this->getXUAT_NGAY_XUAT(), 
	    		$this->getXUAT_TONG_TIEN_PHAI_THU(), 
	    		$this->getXUAT_TONG_TIEN_DA_THU(), 
	    		$this->getXUAT_LY_DO_XUAT(), 
	    		$this->getIS_ACTIVE() 
	    );
    $types = array(
    	PDO::PARAM_INT,
        PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_INT,//Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR,
        PDO::PARAM_INT,//Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
    );
	$lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
	$this->setXUAT_ID($lastInsertID);
    }
}
	
	public function delete($db = null) {
	    if ($db != null) {
	        $sql = "update `xuat`
	                set `is_active`=0
	                where `XUAT_ID` = :XUAT_ID;";
	        $parameters = array(":XUAT_ID");
	        $values = array($this->getXUAT_ID());
	        $types = array(PDO::PARAM_INT);//Kiem tra lai kieu du lieu nhe
	        $result = $db->executeNonQuery($sql, $parameters, $values, $types);
	        return $result;
	    }
	}
	
	public function update($db = null) {
	        if ($db != null) {
	            $sql = "UPDATE `xuat` SET 
			`XUAT_ID`=:XUAT_ID,
				`TRUONG_ID`=:TRUONG_ID,
				`XUAT_NGAY_XUAT`=:XUAT_NGAY_XUAT,
				`XUAT_TONG_TIEN_PHAI_THU`=:XUAT_TONG_TIEN_PHAI_THU,
				`XUAT_TONG_TIEN_DA_THU`=:XUAT_TONG_TIEN_DA_THU,
				`XUAT_LY_DO_XUAT`=:XUAT_LY_DO_XUAT,
				`IS_ACTIVE`=:IS_ACTIVE
			WHERE `XUAT_ID` = :XUAT_ID;";
	            $parameters = array(
			":XUAT_ID",
				":TRUONG_ID",
				":XUAT_NGAY_XUAT",
				":XUAT_TONG_TIEN_PHAI_THU",
				":XUAT_TONG_TIEN_DA_THU",
				":XUAT_LY_DO_XUAT",
				":IS_ACTIVE"
			);
	// Kiem tra dau , nhe
	$values = array(  
			$this->getXUAT_ID(),
				$this->getTRUONG_ID(),
				$this->getXUAT_NGAY_XUAT(),
				$this->getXUAT_TONG_TIEN_PHAI_THU(),
				$this->getXUAT_TONG_TIEN_DA_THU(),
				$this->getXUAT_LY_DO_XUAT(),
				$this->getIS_ACTIVE()
		);
    $types = array(
    	PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_INT,  //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
    	PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
    );
	$result = $db->executeNonQuery($sql, $parameters, $values, $types);
	return $result;
	        }
	 }
	 
	public function getByID($db = null) {
	if ($db != null) {
	$sql = "SELECT
			`XUAT_ID`,`TUAN_ID`,
                        `THANG_ID`,
				`TRUONG_ID`,
				`XUAT_SO_PHIEU_XUAT`,
				`XUAT_NGAY_XUAT`,
				`XUAT_TONG_TIEN_PHAI_THU`,
				`XUAT_TONG_TIEN_DA_THU`,
				`XUAT_LY_DO_XUAT`,
				`IS_ACTIVE`
			FROM `xuat` WHERE `XUAT_ID` = :XUAT_ID and `is_active`=1;";
	$parameters = array(":XUAT_ID");
	$values = array($this->getXUAT_ID());
	$types = array(PDO::PARAM_INT);//kiem tra lai kieu du lieu nhe
	$rows = $db->executeQuery($sql, $parameters, $values, $types);
	if ($rows != null) {
	$row = $rows->fetch();
		$this->setXUAT_ID($row->XUAT_ID);
                $this->setTUAN_ID($row->TUAN_ID);
		$this->setTHANG_ID($row->THANG_ID);
		$this->setTRUONG_ID($row->TRUONG_ID);
		$this->setXUAT_SO_PHIEU_XUAT($row->XUAT_SO_PHIEU_XUAT);
		$this->setXUAT_NGAY_XUAT($row->XUAT_NGAY_XUAT);
		$this->setXUAT_TONG_TIEN_PHAI_THU($row->XUAT_TONG_TIEN_PHAI_THU);
		$this->setXUAT_TONG_TIEN_DA_THU($row->XUAT_TONG_TIEN_DA_THU);
		$this->setXUAT_LY_DO_XUAT($row->XUAT_LY_DO_XUAT);
		$this->setIS_ACTIVE($row->IS_ACTIVE);
	}
	return $this;
		}
	}
	
        
        
        
        
          	public function getMaxID($db = null) {
	if ($db != null) {
	$sql = "SELECT MAX(`xuat`.`XUAT_SO_PHIEU_INT`) AS XUAT_SO_PHIEU_INT, YEAR(CURRENT_DATE) YEAR "
                . "FROM `xuat` "
                . "WHERE `xuat`.`IS_ACTIVE`=1 AND YEAR(`xuat`.`XUAT_NGAY_XUAT`)=YEAR(CURRENT_DATE)";
	$parameters = array();
	$values = array();
	$types = array();//kiem tra lai kieu du lieu nhe
	$rows = $db->executeQuery($sql, $parameters, $values, $types);
	if ($rows != null) {
            $row = $rows->fetch();
            $this->setXUAT_SO_PHIEU_INT($row->XUAT_SO_PHIEU_INT);
            $this->setXUAT_SO_PHIEU_XUAT($row->YEAR);
	}
	return $this;
	}
	}
        
        public function getMaxID_($db = null) {
	if ($db != null) {
	$sql = "SELECT MAX(`xuat`.`XUAT_ID`) AS XUAT_ID FROM `xuat` WHERE `xuat`.`IS_ACTIVE`=1";
	$parameters = array();
	$values = array();
	$types = array();//kiem tra lai kieu du lieu nhe
	$rows = $db->executeQuery($sql, $parameters, $values, $types);
	if ($rows != null) {
            $row = $rows->fetch();
            $this->setXUAT_ID($row->XUAT_ID);
	}
	return $this;
	}
	}
        
        
        
        
             public function getMaxTuan($db = null) {
        if ($db != null) {
            $sql = "SELECT MAX(`xuat`.`TUAN_ID`) AS  TUAN_ID FROM `xuat` WHERE `THANG_ID`=:THANG_ID AND `xuat`.`IS_ACTIVE`=1";
             $parameters = array(":THANG_ID");
            $values = array($this->getTHANG_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTUAN_ID($row->TUAN_ID);
            }
            return $this;
        }
    }
    
    
    
    public function getMaxThang($db = null) {
        if ($db != null) {
            $sql = "SELECT MAX(`xuat`.`THANG_ID`) AS  THANG_ID FROM `xuat` WHERE `xuat`.`IS_ACTIVE`=1";
            $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTHANG_ID($row->THANG_ID);
            }
            return $this;
        }
    }
        
         public function showdulieu($idbep,$thang,$tuan,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`XUAT_TONG_TIEN_PHAI_THU`) AS TONG_TIEN,MAX(`XUAT_NGAY_XUAT`) AS MAX_XUAT_NGAY_XUAT,MIN(`XUAT_NGAY_XUAT`) AS MIN_XUAT_NGAY_XUAT
            FROM `xuat` A JOIN `truong` B
            ON A.`TRUONG_ID` = B.`TRUONG_ID`
            WHERE tuan_id = ".$tuan." AND thang_id = ".$thang." AND BEP_ID = ".$idbep." AND A.`is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setXUAT_TONG_TIEN_PHAI_THU($row->TONG_TIEN);
                $this->setMAX_XUAT_NGAY_XUAT($row->MAX_XUAT_NGAY_XUAT);
                $this->setMIN_XUAT_NGAY_XUAT($row->MIN_XUAT_NGAY_XUAT);
            }
            return $this;
        }
    }
     public function showdulieu_thang($idbep,$thang,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`XUAT_TONG_TIEN_PHAI_THU`) AS TONG_TIEN,MAX(`XUAT_NGAY_XUAT`) AS MAX_XUAT_NGAY_XUAT,MIN(`XUAT_NGAY_XUAT`) AS MIN_XUAT_NGAY_XUAT
            FROM `xuat` A JOIN `truong` B
            ON A.`TRUONG_ID` = B.`TRUONG_ID`
            WHERE thang_id = ".$thang." AND BEP_ID = ".$idbep." AND A.`is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setXUAT_TONG_TIEN_PHAI_THU($row->TONG_TIEN);
                 $this->setMAX_XUAT_NGAY_XUAT($row->MAX_XUAT_NGAY_XUAT);
                $this->setMIN_XUAT_NGAY_XUAT($row->MIN_XUAT_NGAY_XUAT);
            }
            return $this;
        }
    }
     public function showdulieu_tungay_denngay($BEP_ID,$tungay, $denngay,$db = null) {
        if ($db != null) {
            $sql = "SELECT SUM(`XUAT_TONG_TIEN_PHAI_THU`) AS TONG_TIEN,MAX(`XUAT_NGAY_XUAT`) AS MAX_XUAT_NGAY_XUAT,MIN(`XUAT_NGAY_XUAT`) AS MIN_XUAT_NGAY_XUAT,B.`BEP_ID`
            FROM `xuat` A JOIN `truong` B
            ON A.`TRUONG_ID` = B.`TRUONG_ID`
            WHERE `XUAT_NGAY_XUAT`>='".$tungay."' AND `XUAT_NGAY_XUAT`<='".$denngay."' AND `BEP_ID` = ".$BEP_ID." AND A.`is_active`=1;";
           $parameters = array();
            $values = array();
            $types = array(); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setXUAT_TONG_TIEN_PHAI_THU($row->TONG_TIEN);
                 $this->setTRUONG_ID($row->BEP_ID);
            }
            return $this;
        }
    }
        
        
        
        
        
        
        
        
	public function getList($db = null) {
	if ($db != null) {
    $sql = "SELECT
			`XUAT_ID`,
				`TRUONG_ID`,
				`XUAT_SO_PHIEU_XUAT`,
				`XUAT_NGAY_XUAT`,
				`XUAT_TONG_TIEN_PHAI_THU`,
				`XUAT_TONG_TIEN_DA_THU`,
				`XUAT_LY_DO_XUAT`,
				`IS_ACTIVE`
			FROM `xuat` WHERE `is_active`=1 ORDER BY XUAT_ID DESC;";
    $rows = $db->executeQuery($sql);
    $objects = array();
    if ($rows != null) {
    while ($row = $rows->fetch()) {
    $obj = new Xuat();
    	 $obj->setXUAT_ID($row->XUAT_ID);
		 $obj->setTRUONG_ID($row->TRUONG_ID);
		 $obj->setXUAT_SO_PHIEU_XUAT($row->XUAT_SO_PHIEU_XUAT);
		 $obj->setXUAT_NGAY_XUAT($row->XUAT_NGAY_XUAT);
		 $obj->setXUAT_TONG_TIEN_PHAI_THU($row->XUAT_TONG_TIEN_PHAI_THU);
		 $obj->setXUAT_TONG_TIEN_DA_THU($row->XUAT_TONG_TIEN_DA_THU);
		 $obj->setXUAT_LY_DO_XUAT($row->XUAT_LY_DO_XUAT);
		 $obj->setIS_ACTIVE($row->IS_ACTIVE);
		$objects[] = $obj;
    }
    return $objects;
    }
        }
    }
}
?>

